import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import precision_score
#
print('---------第一题加载数据---------')
lir=load_iris();
# print(lir.target_names)
# colorm
X=lir.data
# print(X)
Y=lir.target
#切分数据
print(X.shape)
print('---------第二题切分数据集---------')
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,Y,test_size=0.3,random_state=420)
print(Xtrain.shape)
LR1=LogisticRegression(penalty='l2',solver='liblinear',C=0.5,max_iter=1000);
LR1.fit(Xtrain,Ytrain);
score=LR1.score(Xtest,Ytest);
#得分
print(score)
#归一化
sctand=StandardScaler();
X_stand=sctand.fit_transform(Xtrain);
X_test_stand=sctand.fit_transform(Xtest);
LR2=LogisticRegression(penalty='l2',solver='liblinear',C=0.5,max_iter=1000);
LR2.fit(X_stand,Ytrain);
score=LR2.score(X_test_stand,Ytest);
#得分
print('---------#归一化得分---------')
print(score)
# print(X_stand

#在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合

p={'C':list(np.linspace(0.05,1,19)),
    'solver':['liblinear','lbfgs','newton-cg','sag']}
#'liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'
modle=LogisticRegression(penalty='l2',max_iter=1000)

Gs=GridSearchCV(modle,p,cv=5);

Gs.fit(X_stand,Ytrain)
print('---#网格搜索最优分数---')
print(Gs.best_score_)
print('---#网格搜索最优参数---')
print(Gs.best_params_)

#将最新参数重新用于训练集和测试集

LLR=LogisticRegression(penalty='l2',max_iter=1000,solver=Gs.best_params_['solver'],C=Gs.best_params_['C'])

LLR.fit(X_stand,Ytrain);

Xtrain_score=LLR.score(X_stand,Ytrain)
Xtest_score=LLR.score(X_test_stand,Ytest)
print('---#训练集评分---')
print(Xtrain_score)

print('---#测试集评分---')
print(Xtest_score)

X=sctand.fit_transform(X)
pre=precision_score(Y,LLR.predict(X),average='micro')
print('---#精确率---')
print(pre)








